package com.example.mvvm.ui.my

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.mvvm.data.PlaceRepository
import com.example.mvvm.data.model.place.City
import com.example.mvvm.data.model.place.Province
import kotlinx.coroutines.launch

class MyViewModel(private val placeRepository: PlaceRepository) : ViewModel() {
    var provinces: MutableLiveData<MutableList<Province>> = MutableLiveData()
    var cities: MutableLiveData<MutableList<City>> = MutableLiveData()

    var dataChanged: MutableLiveData<Int> = MutableLiveData()

    var selectedProvince: MutableLiveData<Province> = MutableLiveData()
    var selectedPositon: MutableLiveData<Int> = MutableLiveData()
    var selectedCity: City? = null

    var name = MutableLiveData<String>()

    val myAdapter = MyAdapter()

    var cityAdapter = CityAdapter()

    var test:String  = "1"
    fun getProvinces() {
        //---
        launch {
            provinces.value = placeRepository.getProviceList()

            myAdapter.setNewInstance(provinces.value)
            myAdapter.notifyDataSetChanged()
            myAdapter.setOnItemClickListener { adapter, view, position ->
                provinces.value?.let {
//                    selectedProvince.value = provinces.value!![position]
                    selectedPositon.value = position
                    test = "2"
//                    getCities()
                }
            }
        }
    }

    fun getCities(p: Int) {
        launch {
            if (p != -1) {
                val proviceList = placeRepository.getProviceList()

                proviceList?.let {
                    val province = proviceList!![p]
                    province?.let {
                        val cityList = placeRepository.getCityList(it.id)
                        //这地方的赋值没看懂，别的地方有用么
//                        cities.value = cityList
                        Log.e("wodep1", "cityList=" + cityList.size)
                        cityAdapter.setNewInstance(cityList)
                        cityAdapter.notifyDataSetChanged()
                    }
                }
            } else {

            }
        }
    }

//    fun getCities(p:Int) = selectedProvince?.let {
//
//        launch {
//            it.value?.let {
//                cities.value = placeRepository.getCityList(it.id)
//                Log.e("wode",cities.value?.get(2)!!.cityName)
//            }
//        }
//    }

    fun getCountries() = selectedCity?.let {
        launch {
            placeRepository.getCountryList(it.provinceId, it.cityCode)
        }
    }


//    private fun launch0(block: suspend () -> Unit): () -> String = {
//        "s"
//        "a"
//        "b"
//    }
//
//    private fun launch1(block: suspend () -> Unit) {
//        Log.e("wode","dd")
//        viewModelScope.launch {
//            block()
//        }
//    }


    fun test(s: String): Unit {
        Log.e("wode", "dd")
    }

    class ViewModelFacoty(private val placeRepository: PlaceRepository) :
        ViewModelProvider.Factory {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return MyViewModel(placeRepository) as T
        }

    }
//block: suspend () -> Util

    /*
    如果 Lambda 是函数的最后一个参数，你可以把 Lambda 写在括号的外面：

view.setOnClickListener() { v: View ->
  switchToNextPage()
}

而如果 Lambda 是函数唯一的参数，你还可以直接把括号去了：

view.setOnClickListener { v: View ->
  switchToNextPage()
}

另外，如果这个 Lambda 是单参数的，它的这个参数也省略掉不写：

view.setOnClickListener {
  switchToNextPage()
}

哎，不错，单参数的时候只要不用这个参数就可以直接不写了。

其实就算用，也可以不写，因为 Kotlin 的 Lambda 对于省略的唯一参数有默认的名字：it：
     */
    private fun launch(block: suspend () -> Unit) = viewModelScope.launch {
        block()
        dataChanged.value = dataChanged.value?.plus(1)
    }
//
//    private fun launch1(block: suspend () -> Unit) {
//        viewModelScope.launch {
//            block()
//        }
//    }

    override fun onCleared() {
        super.onCleared()
        Log.e("wode","走了clear")
    }

}